//
//  LLStautsCell.swift
//  xinlangweibo
//
//  Created by 任玉林 on 16/6/27.
//  Copyright © 2016年 任玉林. All rights reserved.
//

import UIKit

import SnapKit

 let OriginalMargin: CGFloat = 10

class LLStautsCell: UITableViewCell {
    
    // 纪录底部toolbar顶部约束
    var toolBarTopConstraint:Constraint?
    
    // 供外界给cell设置数据
    var status: LLStatusesModel? {
        // 当外界给其赋值的时候 会走didSet 方法
        didSet{
            // 给原装微博赋值
            statusOriginalView.status = status
            // 先卸载底部toolbar top 原有约束
            toolBarTopConstraint?.uninstall()
            // 没有转发微博
            if status?.retweeted_status == nil {
                
                statusRetweetView.hidden = true
                // 如果你已经卸载该约束了 重新设置约束 使用make 和updata 一样
                statusTooBarView.snp_makeConstraints(closure: { (make) -> Void in
                    toolBarTopConstraint = make.top.equalTo(statusOriginalView.snp_bottom).constraint
                })
                
            }else {
                // 有转发微博
                // 先赋值
                statusRetweetView.status = status
                statusRetweetView.hidden = false
                
                statusTooBarView.snp_makeConstraints(closure: { (make) -> Void in
                    toolBarTopConstraint = make.top.equalTo(statusRetweetView.snp_bottom).constraint
                })
            }
            // 给toolbar赋值
            statusTooBarView.status = status
            
        }
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style,reuseIdentifier: reuseIdentifier)
    
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置视图
    private func setupUI(){
        
        //添加
        contentView.addSubview(statusOriginalView)
        contentView.addSubview(statusRetweetView)
        contentView.addSubview(statusTooBarView)
        
        statusOriginalView.backgroundColor = UIColor.whiteColor()
        
        self.backgroundColor = RGB(237, green: 237, blue: 237)
        
        //设置约束
        statusOriginalView.snp_makeConstraints { (make) -> Void in
            
            make.trailing.leading.equalTo(contentView)
            
            make.top.equalTo(contentView).offset(10)
            
        }
        
        statusRetweetView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(statusOriginalView.snp_bottom)
            
            make.leading.trailing.equalTo(contentView)
            
            
                    }
        
        statusTooBarView.snp_makeConstraints { (make) -> Void in
            
            toolBarTopConstraint = make.top.equalTo(statusRetweetView.snp_bottom).constraint
            
            make.leading.trailing.equalTo(contentView)

            make.height.equalTo(35)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.trailing.equalTo(self)

            make.bottom.equalTo(statusTooBarView)
        }
    }
    
    //懒加载控件
    private lazy var statusOriginalView: LLStatusOriginalView = LLStatusOriginalView()
    
    private lazy var statusRetweetView: LLStatusRetweetView = LLStatusRetweetView()
    
    private lazy var statusTooBarView: LLStatusTooBarView = LLStatusTooBarView()

}
